package com.example.myapplication.request

import androidx.lifecycle.MutableLiveData
import com.example.myapplication.bean.UserBean
import com.kun.base.net.Api
import com.kun.base.net.ResponseResult

/**
 *
 *
 * @author : Kun
 * @date : 2021/6/4
 * @time : 14:02
 */
class LoginRequest {
    /**
     * 注册返回的结果
     */
    val registerResult = MutableLiveData<ResponseResult<UserBean>>()

    /**
     * 登录返回的结果
     */
    val loginResult = MutableLiveData<ResponseResult<UserBean>>()

    /**
     * 退出返回的结果
     */
    val logoutResult = MutableLiveData<ResponseResult<String>>()

    /**
     * 注册
     */
    suspend fun requestRegister(userName: String, password: String, rePassword: String) {

        val res = Api.apiCall {
            val map = HashMap<String, String>(3)
            map["username"] = userName
            map["password"] = password
            map["repassword"] = rePassword
            Api.getInstance(IApi::class.java).register(map)
        }
        registerResult.postValue(res)
    }

    /**
     * 登录
     */
    suspend fun requestLogin(userName: String, password: String) {

        val res = Api.apiCall {
            val map = HashMap<String, String>(2)
            map["username"] = userName
            map["password"] = password
            Api.getInstance(IApi::class.java).login(map)
        }
        loginResult.postValue(res)
    }

    /**
     * 退出登录
     */
    suspend fun requestLogout() {

        val res = Api.apiCall {
            Api.getInstance(IApi::class.java).logout()
        }
        logoutResult.postValue(res)
    }
}